from pyspark.sql import SparkSession

if __name__ == '__main__':
    spark = SparkSession.builder. \
        appName("test"). \
        master("local[*]"). \
        getOrCreate()

    sc = spark.sparkContext

    df = spark.read.format("csv") \
        .schema("id int, subject string, score int") \
        .load("../data/input/stu_score.txt")

    # 注册临时表
    df.createTempView("score")
    df.createOrReplaceTempView("score_2")
    df.createGlobalTempView("score_3")

    # sql api
    spark.sql("select subject,count(*) as cnt from score group by subject").show()
    spark.sql("select subject,count(*) as cnt from score_2 group by subject").show()
    spark.sql("select subject,count(*) as cnt from global_temp.score_3 group by subject").show()
